package resp

import (
	"encoding/json"
	"net/http"
)

var _ To = &tSimpleResp{}

type tSimpleResp struct {
	Code  int    `json:"code"`
	Msg   string `json:"msg,omitempty"`
	Data  any    `json:"data"`
	Other any    `json:"other,omitempty"`
}

func (r *tSimpleResp) SetCode(code int) {
	r.Code = code
}

func (r *tSimpleResp) SetMsg(msg string) {
	r.Msg = msg
}

func (r *tSimpleResp) SetData(data any) {
	r.Data = data
}

func (r *tSimpleResp) SetOther(other any) {
	r.Other = other
}

func (r *tSimpleResp) ToJson() []byte {
	marshal, _ := json.Marshal(r)
	return marshal
}

func (r *tSimpleResp) Value() any {
	return r
}

func (r *tSimpleResp) Gin() (int, any) {
	return r.Code, r
}

func (r *tSimpleResp) GinOK() (int, any) {
	return http.StatusOK, r
}

func NewSimple() Resp {
	return new(simpleResp)
}

type simpleResp byte

func (*simpleResp) New(code int, msg string, data any, other any) To {
	return &tSimpleResp{
		Code:  code,
		Msg:   msg,
		Data:  data,
		Other: other,
	}
}

func (*simpleResp) NewOK(list ...any) To {
	var rt = &tSimpleResp{Code: http.StatusOK}
	length := len(list)
	switch length {
	case 0:
	case 1:
		rt.Data = list[0]
	case 2:
		rt.Data = list[0]
		rt.Other = list[1]
	default:
		rt.Data = list[0]
		rt.Other = list[1:]
	}
	return rt
}

func (*simpleResp) NewErr(code int, msg string) To {
	return &tStdResp{
		Code: code,
		Msg:  msg,
	}
}
